package com.white.commonlib.manager;

import android.util.Log;

import com.white.commonlib.BuildConfig;
import com.white.commonlib.base.AppContext;
import com.white.commonlib.utils.FileUtils;
import com.white.ndkmaster.NativeX;

import java.io.File;

/**
 * Created by ytf on 2017/3/14 014.
 * Description: log管理类
 */

public class LogManager
{
    private static boolean showLog = BuildConfig.DEBUG;
    private static final String TAG = "s";

    public static void setDebug(boolean yn)
    {
        showLog = yn;
    }

    static int policy = 0;
    public static void setPolicy(int p)
    {
        policy = p;
    }

//    private static String callMethodAndLine()
//    {
//        String result = "at ";
//        StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
//        result += thisMethodStack.getClassName()+ "."; //  当前的类名（全名）
//        result += thisMethodStack.getMethodName();
//        result += "(" + thisMethodStack.getFileName();
//        result += ":" + thisMethodStack.getLineNumber() + ")  ";
//        return result;
//    }
    public static final int MAX_LOG_LENGTH = 2000;

    public static void e(String msg)
    {
        if(showLog)
        {
            if(msg != null)
            {
                long length = msg.length();
                if(length > MAX_LOG_LENGTH)
                {
                    int size = (int) (length / MAX_LOG_LENGTH);
                    int mode = (int) (length % MAX_LOG_LENGTH);
                    int start = 0;
                    int end = 0;
                    for (int i=0; i< size;i++)
                    {
                        end = (i+1) * MAX_LOG_LENGTH;
                        String cs = msg.substring(start, end);
                        start = end;
                        Log.e(TAG, cs);
                    }
                    if(mode != 0)
                    {
                        String endString = msg.substring(size*MAX_LOG_LENGTH, msg.length());
                        Log.e(TAG, endString);
                    }
                }else
                {
                    Log.e(TAG, msg );
                }
            }
        }
    }

    public static void write(String string)
    {
        String logPath = FileUtils.createPathBasedOnApp(AppContext.getContext(), "/line");
        File f = new File(logPath, "line.log");
        try
        {
            if(!f.exists())
                f.createNewFile();
            NativeX.writeString(logPath, string);
        }catch (Exception e){}
    }

    public void e(String tag, String msg)
    {
        if(showLog)
            Log.e(tag, msg);
    }

    public static void d(String msg)
    {
        if(showLog)
            Log.d(TAG, msg);
    }

    public static void d(String tag, String msg)
    {
        if(showLog)
            Log.d(tag, msg);
    }
    public static void i(String msg)
    {
        if(showLog)
            Log.i(TAG, msg);
    }

    public static void i(String tag, String msg)
    {
        if(showLog)
            Log.i(tag, msg);
    }

    public static void v(String msg)
    {
        if(showLog)
            Log.v(TAG, msg);
    }

    public static void v(String tag, String msg)
    {
        if(showLog)
            Log.v(tag, msg);
    }
}
